package com.snda.lib.http;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.snda.lib.task.BaseAsyncTask;
import android.os.Bundle;
import android.util.Log;

public class TaskManager {

	private Integer nCookie = 1000;

	// reqList中存放所有请求的Url
	private LinkedList<TaskData> reqList = null;
	private Map<Integer, BaseAsyncTask> mapTask = null;
	private List<Long> listId = null; // 保存所有的正在运行的task的ID

	private static TaskManager manager = new TaskManager();

	public static TaskManager getInstance() {
		return manager;
	}

	private TaskManager() {
		reqList = new LinkedList<TaskData>();
		mapTask = new HashMap<Integer, BaseAsyncTask>();
		listId = new ArrayList<Long>();
	}

	public void RemoveTask(int nCookie) {
		BaseAsyncTask task = mapTask.get(nCookie);
		if (task == null)
			return;

		long nId = task.getId();
		listId.remove(nId);
		mapTask.remove(nCookie);
	}

	public BaseAsyncTask getTask(int nCookie) {
		return mapTask.get(nCookie);
	}

	public synchronized Integer AddCookie() {

		int nCookie = ++TaskManager.getInstance().nCookie;
		return nCookie;
	}

	public Integer GetCookie() {
		return TaskManager.getInstance().nCookie;
	}

	public Integer PushTask(TaskData reqObj) {
		if (reqObj == null)
			return 0;

		int nCookie = AddCookie();
		reqObj.nCookie = nCookie;

		// 该ID已经存在
		long nId = Math.abs(reqObj.task.getId());
		if (reqObj.bAllowRepeated == false) {

			for (int i = 0; i < listId.size(); i++) {
				if (nId == listId.get(i)) {
					return 0;
				}
			}
		}

		if (reqObj.nCookie == 0) {
			Log.d("SNLib", "Cookie:" + reqObj.nCookie);
		}

		reqList.offer(reqObj);
		return reqObj.nCookie;
	}

	public Integer ContinueTask(int nCookie, TaskData reqObj) {

		long nId = reqObj.task.getId();
		if (reqObj.bAllowRepeated == false) {
			for (int i = 0; i < listId.size(); i++) {
				if (nId == listId.get(i)) {
					return 0;
				}
			}
		}

		reqObj.nCookie = nCookie;
		reqList.offer(reqObj);
		return nCookie;
	}

	public TaskData PopTask() {
		if (reqList == null)
			return null;

		if (reqList.size() == 0)
			return null;

		TaskData data = reqList.poll();
		if (data == null)
			return null;

		return data;
	}

	private void ExecuteTask(TaskData taskData) {

		if (taskData == null)
			return;

		if (taskData.nCookie == 0) {
			Log.d("SNLib", "Cookie:" + nCookie);
		}

		taskData.task.execute(taskData.params, taskData.nCookie);
		mapTask.put(taskData.nCookie, taskData.task);
		taskData.task = null;
	}

	public void ExecuteTaskReq() {
		int nSendNum = 0;
		while (nSendNum < 3) {

			TaskData taskObj = (TaskData) PopTask();
			if (taskObj != null) {
				ExecuteTask(taskObj);
			} else {
				break;
			}
			nSendNum++;
		}
	}

	// ================================================
	// 这两个接口为对库外提供的接口
	// ================================================

	// 增加一个Task
	public static int addTask(BaseAsyncTask task, Bundle params,
			boolean bAllowRepeated) {
		TaskData data = new TaskData(task, params);
		data.bAllowRepeated = bAllowRepeated;
		return TaskManager.getInstance().PushTask(data);
	}

	// 将一个已有的Task继续
	public static int continueTask(int nCookie, BaseAsyncTask task,
			Bundle params, boolean bAllowRepeated) {
		TaskData data = new TaskData(task, params);
		data.bAllowRepeated = bAllowRepeated;
		return TaskManager.getInstance().ContinueTask(nCookie, data);
	}
}